package com.xudow.app.newui.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bokecc.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.DWLiveLoginListener;
import com.bokecc.sdk.mobile.live.Exception.DWLiveException;
import com.bokecc.sdk.mobile.live.pojo.RoomInfo;
import com.bokecc.sdk.mobile.live.pojo.TemplateInfo;
import com.bokecc.sdk.mobile.live.pojo.Viewer;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplay;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplayLoginListener;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.directseeding.LiveReplayActivity;
import com.xudow.app.directseeding.LiveRoomActivity;
import com.xudow.app.directseeding.entity.LiveRoom;
import com.xudow.app.newui.DirectseedingActivity;
import com.xudow.app.newui.PreLoginActivity;
import com.xudow.app.newui.zutils.LsUtils;
import com.xudow.app.util.ImageUtils;

import java.util.List;

/**
 * 作者：LownChen on 2016/10/27 16:20
 */
public class DirectseedingAdapter extends BaseAdapter{
    Context context;
    List<LiveRoom.ScreenEntity> screen;
    public DirectseedingAdapter(Context context, List<LiveRoom.ScreenEntity> screen) {
        this.context=context;
        this.screen=screen;
    }

    @Override
    public int getCount() {
        return screen.size();
    }

    @Override
    public Object getItem(int position) {
        return screen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

            //直播
       private DWLive dwLive = DWLive.getInstance();

        private DWLiveLoginListener dwLiveLoginListener = new DWLiveLoginListener() {

            @Override
            public void onLogin(TemplateInfo info, Viewer viewer, RoomInfo roomInfo) {

                LsUtils.pdDismiss();
                Intent intent = new Intent(context, LiveRoomActivity.class);

                Bundle bundle = new Bundle();

                bundle.putString("chat", info.getChatView());
                bundle.putString("pdf", info.getPdfView());
                bundle.putString("qa", info.getQaView());
                bundle.putString("jj","1");
                bundle.putString("desc",desc);
                bundle.putString("imagpath",imagpath);
                bundle.putString("name",name);


                intent.putExtras(bundle);

                context.startActivity(intent);
            }

            @Override
            public void onException(DWLiveException exception) {
//			Message msg = new Message();
//			msg.what = -1;
//			msg.obj = exception.getMessage();
//			mHandler.sendMessage(msg);
            }
        };
        //回放
        private DWLiveReplay dwLiveReplay = DWLiveReplay.getInstance();
        private DWLiveReplayLoginListener dwLiveReplayLoginListener = new DWLiveReplayLoginListener() {

            @Override
            public void onLogin(TemplateInfo templateInfo) {
                LsUtils.pdDismiss();
                Intent intent = new Intent(context, LiveReplayActivity.class);

                Bundle bundle = new Bundle();
                bundle.putString("chat", templateInfo.getChatView());
                bundle.putString("pdf", templateInfo.getPdfView());
                bundle.putString("qa", templateInfo.getQaView());
                bundle.putString("jianjie", desc);
                bundle.putString("imagpath",imagpath);
                bundle.putString("name",name);

                intent.putExtras(bundle);

                context.startActivity(intent);
            }

            @Override
            public void onException(DWLiveException exception) {
//			Message msg = new Message();
//			msg.what = -2;
//			msg.obj = exception.getMessage();
//			mHandler.sendMessage(msg);
            }
        };


     String desc;
    String name;
    String imagpath;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_directseeding,null);
            holder=new MyViewHolder(convertView);
            convertView.setTag(holder);

        }else{
            holder=(MyViewHolder)convertView.getTag();
        }
        ImageUtils.loadImage(context, holder.imagepath, Config.BASE_URL + "imagedelivery?name=" + screen.get(position).getImgPath(), R.mipmap.xuedou_zhibo);
        holder.liveroom_text.setText(screen.get(position).getName());
//            ImageLoader.getInstance().displayImage(Config.BASE_URL + "imagedelivery?name=" + screen.get(position).getImgPath(), holder.imagepath);
            if(!screen.get(position).getLiveId().equals("10")){
            holder.zbhfbs.setImageResource(R.mipmap.pr_huifangzhong);
            }else{
                holder.zbhfbs.setImageResource(R.drawable.pr_zhibozhong);
            }
        ImageUtils.loadImage(context, holder.jigou_iv, Config.BASE_URL + "imagedelivery?name=" + screen.get(position).getPhotoPath(), R.mipmap.default_portrait);
//            ImageLoader.getInstance().displayImage(Config.BASE_URL + "imagedelivery?name=" + screen.get(position).getPhotoPath(), holder.jigou_iv);
            holder.jigou_tv.setText(screen.get(position).getNickName());

          holder.imagepath.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                    LsUtils.pdShou(context, "正在进入直播间");
//                    XApplication application=(XApplication)congetApplication();
//                    if (!application.isLogin()) {
//                        LsUtils.pdDismiss();
//
//                        startActivity(new Intent(DirectseedingActivity.this, PreLoginActivity.class));
//
//                    }else{


                  if (!screen.get(position).getLiveId().equals("10")) {
                            desc= screen.get(position).getDesc();
                            imagpath=Config.BASE_URL + "imagedelivery?name=" + screen.get(position).getPhotoPath();
                            name=screen.get(position).getNickName();
//                            Log.i("zhibocanshu", "roomid>>>>>>>" + data.get(position).getRoomId() + "liveid>>>>>>>>" + data.get(position).getLiveId());
                            dwLiveReplay.setLoginParams(dwLiveReplayLoginListener, "BAC7325D2DAFDBA1", screen.get(position).getRoomId(), screen.get(position).getLiveId(), screen.get(position).getDesc());

                            dwLiveReplay.startLogin();

                        } else {
                            desc = screen.get(position).getDesc();
                            imagpath=Config.BASE_URL + "imagedelivery?name=" + screen.get(position).getPhotoPath();
                            name=screen.get(position).getNickName();
//                            SharedPreferences sp = context.getSharedPreferences("UserName",
//                                    context.MODE_PRIVATE);
//                            String name2= sp.getString("Name", "");
                            if( screen.get(position).getUserNickName()!=null){

                                dwLive.setDWLiveLoginParams(dwLiveLoginListener, "BAC7325D2DAFDBA1", screen.get(position).getRoomId(),  screen.get(position).getUserNickName());
                            }else{
                                dwLive.setDWLiveLoginParams(dwLiveLoginListener, "BAC7325D2DAFDBA1", screen.get(position).getRoomId(), "学兜用户");
                            }

                            dwLive.startLogin();
                        }

                }
          });

        return convertView;
    }


    class MyViewHolder {
             ImageView imagepath,zbhfbs,jigou_iv;
            TextView liveroom_text,jigou_tv;
            public MyViewHolder(View view)
            {


                liveroom_text=(TextView)view.findViewById(R.id.liveroom_text);
                imagepath=(ImageView)view.findViewById(R.id.imagepath);
                zbhfbs=(ImageView)view.findViewById(R.id.zbhfbs);
                jigou_iv=(ImageView)view.findViewById(R.id.jigou_iv);
                jigou_tv=(TextView)view.findViewById(R.id.jigou_tv);

            }
        }


}
